# -*- encoding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

import time
from datetime import datetime

from openerp.osv import fields, osv
from openerp.tools.translate import _

class switch_customer_payment_no(osv.osv):
    _name = 'switch.customer.payment.no'
    _description = 'Switch Customer Payment No.'
    
    _columns = {
        'cust_payment1':fields.many2one('account.voucher', '1st Customer Payment No.', required=True),
        'cust_payment2':fields.many2one('account.voucher', '2nd Customer Payment No.', required=True),
     }
    
    def switch_no(self, cr, uid, ids, context=None):
        if context is None:
            context = {}
        
        voucher_obj = self.pool.get('account.voucher')
        voucher_line_obj = self.pool.get('account.voucher.line')
        move_obj = self.pool.get('account.move')
        move_line_obj = self.pool.get('account.move.line')
        
        for sp in self.browse(cr, uid, ids, context=context):
            if not ids:
                return []

            payment1 = sp.cust_payment1.number or ''
            payment2 = sp.cust_payment2.number or ''
            payment_ref1 = sp.cust_payment1.move_id.ref or ''
            payment_ref2 = sp.cust_payment2.move_id.ref or ''
            
            if payment1 == payment2:
                raise osv.except_osv(_('Warning!'), _('You cannot switch between customer payment no that have same payment no'))
            if sp.cust_payment1.journal_id.id != sp.cust_payment2.journal_id.id:
                raise osv.except_osv(_('Warning!'), _('You cannot switch between customer payment no that have different journal id'))
            
            voucher_obj.write(cr, uid, sp.cust_payment1.id, {'number': payment2}, context=context)
            for sp1_line in sp.cust_payment1.line_ids:
                voucher_line_obj.write(cr, uid, sp1_line.id, {'name': payment2}, context=context)
            if sp.cust_payment1.move_id:
                move_obj.write(cr, uid, sp.cust_payment1.move_id.id, {'name': payment2, 'ref': payment_ref2}, context=context)
                for sp1_move_line in sp.cust_payment1.move_id.line_id:
                    move_line_obj.write(cr, uid, sp1_move_line.id, {'name': payment2, 'ref': payment_ref2}, context=context)

            voucher_obj.write(cr, uid, sp.cust_payment2.id, {'number': payment1}, context=context)
            for sp2_line in sp.cust_payment2.line_ids:
                voucher_line_obj.write(cr, uid, sp2_line.id, {'name': payment1}, context=context)
            if sp.cust_payment2.move_id:
                move_obj.write(cr, uid, sp.cust_payment2.move_id.id, {'name': payment1, 'ref': payment_ref1}, context=context)
                for sp2_move_line in sp.cust_payment2.move_id.line_id:
                    move_line_obj.write(cr, uid, sp2_move_line.id, {'name': payment1, 'ref': payment_ref1}, context=context)
        
        return True
    
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
